package com.example.diary

import DiaryDbHelper
import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.view.MenuItem
import android.widget.Button
import android.widget.TextView
import android.widget.Toast
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.app.AppCompatActivity
import com.example.R

class DiaryDetailActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_diary_detail)
        // 返回键
        val actionBar = supportActionBar
        if (actionBar != null) {
            actionBar.setHomeButtonEnabled(true)
            actionBar.setDisplayHomeAsUpEnabled(true)
        }


        // 为修改按钮和删除按钮添加点击事件
        val editButton: Button = findViewById(R.id.edit_button)
        val deleteButton: Button = findViewById(R.id.delete_button)
        editButton.setOnClickListener {
            val id = intent.getIntExtra("id", 0)
            val intent = Intent(this, DiaryEditActivity::class.java)
            intent.putExtra("id", id)
            startActivity(intent)
        }
        deleteButton.setOnClickListener { deleteDiary(this) }
    }

    override fun onResume() {
        super.onResume()
        val id = intent.getIntExtra("id", 0)
        var tmpTitle = "no title"
        var tmpDescription = "nothing"

        val dbHelper = DiaryDbHelper(this, "diary.db", 1)
        val db = dbHelper.writableDatabase
        val cursor = db.rawQuery("select * from diary where id = $id", null)
        if(cursor.moveToFirst()) {
            do {
                tmpTitle = cursor.getString(cursor.getColumnIndexOrThrow("title"))
                tmpDescription = cursor.getString(cursor.getColumnIndexOrThrow("description"))
            } while (cursor.moveToNext())
        }
        val titleTextView: TextView = findViewById(R.id.title_textView)
        val descriptionTextView: TextView = findViewById(R.id.description_textView)
        titleTextView.text = tmpTitle
        descriptionTextView.text = tmpDescription
        cursor.close()
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        when (item.itemId) {
            android.R.id.home -> {
                this.finish()
                return true
            }
        }
        return super.onOptionsItemSelected(item)
    }

    private fun deleteDiary(activity: Activity) {
        AlertDialog.Builder(this).apply {
            setTitle("删除日记")
            setMessage("您确定要删除这篇日记吗？")
            setCancelable(false)
            setPositiveButton("确定") { _, _ ->
                // 从数据库中删除日记
                val id = intent.getIntExtra("id", 0)
                val dbHelper = DiaryDbHelper(activity, "diary.db", 1)
                val db = dbHelper.writableDatabase
                db.delete("diary", "id = ?", arrayOf(id.toString()))
                Toast.makeText(activity, "日记已删除", Toast.LENGTH_SHORT).show()
                activity.finish()
            }
            setNegativeButton("取消") { _, _ -> }
        }.show()
    }

}